package org.bozo.im.activity.roster;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Hashtable;

import org.bozo.im.R;
import org.bozo.im.activity.chat.ChatActivity;
import org.bozo.im.activity.roster.RawAdapter.RawContactHolder;
import org.bozo.im.common.types.Protocol;
import org.bozo.im.common.types.RawContact;
import org.bozo.im.common.utils.PresenceUtils;

import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.ContactsContract.Contacts;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.QuickContactBadge;
import android.widget.TextView;

public class ContactAdapter extends BaseAdapter implements OnClickListener {

	private static ContactComparator mComparator = new ContactComparator();

	private ArrayList<ContactAggregator> mContacts;
	private Context mContext;
	private LayoutInflater mInflater;

	private AlertDialog mDialog;
	
	public ContactAdapter(Context pContext) {
		mContext = pContext;
		mContacts = new ArrayList<ContactAggregator>();
	}

	public int getCount() {
		return mContacts.size();
	}

	public Object getItem(int pPosition) {
		return mContacts.get(pPosition);
	}

	public long getItemId(int pPosition) {
		return mContacts.get(pPosition).hashCode();
	}

	public View getView(int pPosition, View pConvertView, ViewGroup pParent) {

		View lView = pConvertView;
		AggregateContactHolder lHolder;

		if (mInflater == null)
			mInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		if (lView == null) {

			lView = mInflater.inflate(R.layout.contact_item, null);
			lView.setClickable(true);
			lView.setOnClickListener(this);

			lHolder = new AggregateContactHolder();

			lHolder.photo = (QuickContactBadge)lView.findViewById(R.id.contact_icon);
			lHolder.name = (TextView)lView.findViewById(R.id.contact_name);
			lHolder.status = (TextView)lView.findViewById(R.id.contact_status);
			lHolder.presence = (ImageView)lView.findViewById(R.id.contact_presence);

			lView.setTag(lHolder);
		} else {
			lHolder = (AggregateContactHolder)lView.getTag();
		}

		ContactAggregator lContact = (ContactAggregator)getItem(pPosition);

		if (lContact != null) {
			lHolder.photo.assignContactUri(ContentUris.withAppendedId(Contacts.CONTENT_URI, lContact.getSystemId()));
			lHolder.name.setText(lContact.getDisplayName());
			lHolder.status.setText(mContext.getResources().getString(PresenceUtils.toLocalizationId(lContact.getPresence())));
			lHolder.presence.setImageResource(PresenceUtils.toImageId(lContact.getPresence()));
			lHolder.contact = lContact;
		}

		return lView;
	}

	@Override
	public boolean isEmpty() {
		return mContacts.isEmpty();
	}

	public void setList(ArrayList<RawContact> pRoster) {

		mContacts.clear();

		Hashtable<Long, ContactAggregator> lContacts = new Hashtable<Long, ContactAggregator>();

		for (RawContact lContact : pRoster) {
			ContactAggregator lAggregator = lContacts.get(lContact.getContactId());

			if (lAggregator == null) {
				lAggregator = new ContactAggregator();
				lContacts.put(lContact.getContactId(), lAggregator);
				mContacts.add(lAggregator);
			}

			lAggregator.addContact(lContact);
		}

		Collections.sort(mContacts, mComparator);

		notifyDataSetChanged();
	}

	private static class AggregateContactHolder {
		QuickContactBadge photo;
		TextView name;
		TextView status;
		ImageView presence;
		ContactAggregator contact;
	}

	public void onClick(View pView) {
		String pLogin = null;
		Protocol pProtocol = null;

		if (pView.getTag() instanceof AggregateContactHolder) {
			AggregateContactHolder lHolder = (AggregateContactHolder)pView.getTag();
			ArrayList<RawContact> lContacts = lHolder.contact.getContacts();

			if (lContacts != null) {
				if (lContacts.size() > 1) {
					mDialog = new AlertDialog.Builder(mContext)
						.setTitle(mContext.getResources().getString(R.string.presence_available))
						.setAdapter(new RawAdapter(mContext, lHolder.contact, this), null)
						.create();
					mDialog.show();
				} else if (lContacts.size() == 1) {
					pLogin = lContacts.get(0).getId();
					pProtocol = lContacts.get(0).getProtocol();
				}
			}
		} else if (pView.getTag() instanceof RawContactHolder){
			mDialog.dismiss();
			RawContactHolder lHolder = (RawContactHolder)pView.getTag();
			pLogin = lHolder.handle;
			pProtocol = lHolder.protocol;
		}

		if (pLogin != null && pProtocol != null) {
			Intent lIntent = new Intent();
			lIntent.setAction(Intent.ACTION_SENDTO);
			lIntent.addCategory(Intent.CATEGORY_DEFAULT);
			lIntent.setData(Uri.parse("imto://" + pProtocol.toIntentProtocol() + "/" + pLogin));
			lIntent.setComponent(new ComponentName(mContext, ChatActivity.class));
			mContext.startActivity(lIntent);
		}
	}
}
